package net.jjjshop.front.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.entity.user.UserAddress;
import net.jjjshop.common.service.settings.RegionService;
import net.jjjshop.common.util.buer.BuerUtil;
import net.jjjshop.framework.common.api.ApiCode;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.log.annotation.OperationLog;
import net.jjjshop.front.controller.BaseController;
import net.jjjshop.front.param.user.UserAddressParam;
import net.jjjshop.front.param.user.UserAuthParam;
import net.jjjshop.front.param.user.UserZFBAddressParam;
import net.jjjshop.front.service.user.UserAddressService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(value = "user", tags = {"I-小程序-用户认证相关接口(人脸,OCR,身份证)"})
@RestController
@RequestMapping("/front/user/auth")
public class UserAuthController extends BaseController {

    @Autowired
    private UserAddressService userAddressService;
    @Autowired
    private RegionService regionService;

    @RequestMapping(value = "/idOCR", method = RequestMethod.GET)
    @OperationLog(name = "身份证OCR")
    @ApiOperation(value = "(1)身份证OCR(上传身份证图片URL", response = String.class)
    public ApiResult<Map<String, Object>> idOCR(String idImageUrl) {
        return ApiResult.ok(BuerUtil.idOCR(idImageUrl));
    }

    @RequestMapping(value = "/authID", method = RequestMethod.POST)
    @OperationLog(name = "提交身份证人像面URL,身份证号,姓名")
    @ApiOperation(value = "(2)提交身份证人像面URL,身份证号,姓名", response = String.class)
    public ApiResult<String> add(@Validated @RequestBody UserAuthParam userAddressParam) {
        if (StringUtils.isBlank(userAddressParam.getIdFaceUrl()) || StringUtils.isBlank(userAddressParam.getName()) || StringUtils.isBlank(userAddressParam.getIdNumber())) {
            return ApiResult.fail( "资料不完整");
        }

        boolean update = userService.update(new LambdaUpdateWrapper<User>().eq(User::getUserId, this.getUser(true).getUserId())
                .set(User::getIdAuthStatus, 20)
                .set(User::getIdFaceUrl, userAddressParam.getIdFaceUrl())
                .set(User::getIdAuthTime, new Date())
                .set(User::getIdNumber, userAddressParam.getIdNumber())
                .set(User::getIdName, userAddressParam.getName()));
        if (update == true) {
            return ApiResult.ok( "认证成功");
        }else {
            return ApiResult.fail( "认证失败");
        }
    }

    @RequestMapping(value = "/getFaceAuthUrl", method = RequestMethod.GET)
    @OperationLog(name = "获取人脸认证H5 url")
    @ApiOperation(value = "(3)获取人脸认证H5 url", response = String.class)
    public ApiResult<Map<String, Object>> getFaceAuthUrl() {
        if (StringUtils.isBlank(this.getUser(true).getIdName())||StringUtils.isBlank(this.getUser(true).getIdNumber())) {
            return ApiResult.fail("未上传身份证信息",null);
        }
        JSONObject data = BuerUtil.kexinrenzheng(this.getUser(true).getIdName(), this.getUser(true).getIdNumber());
        Map<String, Object> result = new HashMap<>();
        result.put("h5Url", data.getString("certifyUrl"));
        boolean update = userService.update(new LambdaUpdateWrapper<User>().eq(User::getUserId, this.getUser(true).getUserId())
                .set(User::getIdFaceAuthOrderId, data.getString("certifyId")));
        return ApiResult.ok(result);

    }
    @RequestMapping(value = "/getAuthStatus", method = RequestMethod.GET)
    @OperationLog(name = "获取认证状态")
    @ApiOperation(value = "(4)获取认证状态,idAuthStatus:身份证,idFaceStatus:人脸; 状态:10未认证 20已认证 30认证失败", response = String.class)
    public ApiResult<Map<String, Object>> getAuthStatus() {
        User user = userService.getById(this.getUser(true).getUserId());

         Map<String, Object> result = new HashMap<>();
        result.put("idAuthStatus", user.getIdAuthStatus());
        result.put("idFaceStatus", user.getIdFaceStatus());
        return ApiResult.ok(result);

    }

}
